/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package post.ui;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import post.Client;
import post.Payment;

/**
 *
 * @author Stephen
 */
public class testUI {

    private static String cName, upc, invoice;
    private static int quantity, payment, cardNum;
    private static float amountTendered;
    private static Client client;

    public static void addItem() {

        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

            System.out.println("Enter product " + " upc: ");
            upc = br.readLine();
            System.out.println("Enter quantity: ");
            quantity = Integer.parseInt(br.readLine());
            client.addItemToCart(upc, quantity);
        } catch (Exception e) {
            System.err.println("Error: " + e.toString());
        }
    }

    public static void newTransaction() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

            amountTendered = 0.F;
            cardNum = 0000;
            invoice = "";
            
            System.out.println("Enter customer name: ");
            cName = br.readLine();

            addItem();

            System.out.println();
            boolean cont = true;
            while (cont) {
                System.out.println("Add more products?");
                System.out.println("1 - Yes");
                System.out.println("2 - No");
                int moreProd = Integer.parseInt(br.readLine());

                switch (moreProd) {
                    case 1:
                        addItem();
                        break;

                    case 2:
                        cont = false;
                        break;

                    default:
                        System.out.println("Please choose a valid option");
                }
            }

            System.out.println("\nTotal is " + client.getTotal());

            System.out.println("\nEnter payment method: ");
            System.out.println("1 -- Cash");
            System.out.println("2 -- Credit Card");
            payment = Integer.parseInt(br.readLine());

            switch (payment) {
                case 1:
                    System.out.println("Enter amount tendered: ");
                    amountTendered = Float.parseFloat(br.readLine());
                    invoice = client.checkOut(cName, Payment.Cash, amountTendered, cardNum);
                    break;

                case 2:
                    System.out.println("Enter credit card number: ");
                    cardNum = Integer.parseInt(br.readLine());
                    invoice = client.checkOut(cName, Payment.CreditCard, amountTendered, cardNum);
                    break;

                default:
            }

            System.out.println("Transaction complete...");
            System.out.println(invoice);

        } catch (Exception e) {
            System.err.println("Error: " + e.toString());
        }
    }

    public static void main(String args[]) {

        client = new Client();
        client.init();

        boolean more = true;
        while (more) {
            try {
                newTransaction();
                System.out.println("\nDo another transaction?");
                System.out.println("1 - Yes");
                System.out.println("2 - No");
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                int choice = Integer.parseInt(br.readLine());
                if (choice == 2) {
                    more = false;
                }
            } catch (Exception e) {
                System.err.println("ERROR: " + e.toString());
            }

        }
    }
}
